轻松聘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

279 lines
7.3 KiB

import { Ionicons } from '@expo/vector-icons';
import { useLocalSearchParams, useRouter } from 'expo-router';
import {
ActivityIndicator,
Pressable,
ScrollView,
StyleSheet,
Switch,
Text,
View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { usePrivacyDetailSetting } from '../../../src/hooks';
import { colors } from '../../../src/theme';
import type { PrivacyDetailOption } from '../../../src/types';
/* ===================== 子组件:顶部导航 ===================== */
function Header({ title, onBack }: { title: string; onBack: () => void }) {
return (
<View style={styles.header}>
<Pressable style={styles.headerBtn} onPress={onBack} hitSlop={8}>
<Ionicons name="chevron-back" size={24} color={colors.textPrimary} />
</Pressable>
<Text style={styles.headerTitle} numberOfLines={1}>
{title}
</Text>
<View style={styles.headerBtn} />
</View>
);
}
/* ===================== 子组件:可选项行 ===================== */
function OptionRow({
item,
disabled,
isLast,
onToggle,
}: {
item: PrivacyDetailOption;
disabled: boolean;
isLast: boolean;
onToggle: (id: string, enabled: boolean) => void;
}) {
return (
<View style={[styles.row, !isLast && styles.rowBorder]}>
<View style={styles.rowBody}>
<Text style={[styles.rowTitle, disabled && styles.rowTitleDisabled]}>{item.title}</Text>
{item.description ? <Text style={styles.rowDesc}>{item.description}</Text> : null}
</View>
<Switch
value={item.enabled}
disabled={disabled}
onValueChange={(next) => onToggle(item.id, next)}
trackColor={{ false: colors.border, true: colors.primaryBorder }}
thumbColor={item.enabled ? colors.primary : colors.white}
/>
</View>
);
}
/* ===================== 页面 ===================== */
export default function PrivacyDetailScreen() {
const params = useLocalSearchParams();
const rawId = params.id;
const id = typeof rawId === 'string' ? rawId : Array.isArray(rawId) ? rawId[0] ?? '' : '';
const router = useRouter();
const { detail, loading, error, refresh, toggle } = usePrivacyDetailSetting(id);
if (loading) {
return (
<SafeAreaView style={styles.safeArea} edges={['top']}>
<Header title="隐私设置" onBack={() => router.back()} />
<View style={styles.stateBox}>
<ActivityIndicator size="large" color={colors.primary} />
</View>
</SafeAreaView>
);
}
if (error || !detail) {
return (
<SafeAreaView style={styles.safeArea} edges={['top']}>
<Header title="隐私设置" onBack={() => router.back()} />
<View style={styles.stateBox}>
<Ionicons name="alert-circle-outline" size={40} color={colors.danger} />
<Text style={styles.stateText}>{detail === null ? '暂无该设置项' : '加载失败,请重试'}</Text>
<Pressable style={styles.retryBtn} onPress={refresh}>
<Text style={styles.retryText}>重试</Text>
</Pressable>
</View>
</SafeAreaView>
);
}
return (
<SafeAreaView style={styles.safeArea} edges={['top', 'bottom']}>
<Header title={detail.title} onBack={() => router.back()} />
<ScrollView
style={styles.scroll}
contentContainerStyle={styles.content}
showsVerticalScrollIndicator={false}
>
<View style={styles.introCard}>
<Text style={styles.introText}>{detail.intro}</Text>
</View>
<View style={styles.section}>
<Text style={styles.sectionTitle}>主开关</Text>
<View style={styles.card}>
<View style={styles.row}>
<View style={styles.rowBody}>
<Text style={styles.rowTitle}>{detail.master.title}</Text>
</View>
<Switch
value={detail.master.enabled}
onValueChange={(next) => toggle(detail.master.id, next)}
trackColor={{ false: colors.border, true: colors.primaryBorder }}
thumbColor={detail.master.enabled ? colors.primary : colors.white}
/>
</View>
</View>
</View>
<View style={styles.section}>
<Text style={styles.sectionTitle}>细选项</Text>
<View style={styles.card}>
{detail.options.map((item, index) => (
<OptionRow
key={item.id}
item={item}
disabled={!detail.master.enabled}
isLast={index === detail.options.length - 1}
onToggle={toggle}
/>
))}
</View>
</View>
{detail.note ? (
<View style={styles.noteCard}>
<Ionicons name="information-circle-outline" size={18} color={colors.info} />
<Text style={styles.noteText}>{detail.note}</Text>
</View>
) : null}
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: colors.pageBg,
},
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 8,
height: 48,
backgroundColor: colors.white,
borderBottomWidth: 1,
borderBottomColor: colors.borderLight,
},
headerBtn: {
width: 40,
height: 40,
alignItems: 'center',
justifyContent: 'center',
},
headerTitle: {
flex: 1,
textAlign: 'center',
fontSize: 17,
fontWeight: '700',
color: colors.textPrimary,
},
scroll: {
flex: 1,
},
content: {
padding: 8,
paddingBottom: 24,
},
stateBox: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
gap: 12,
},
stateText: {
marginTop: 12,
fontSize: 14,
color: colors.textSecondary,
},
retryBtn: {
marginTop: 16,
paddingHorizontal: 24,
paddingVertical: 8,
borderRadius: 18,
backgroundColor: colors.primaryBtn,
},
retryText: {
fontSize: 13,
fontWeight: '600',
color: colors.white,
},
introCard: {
backgroundColor: colors.infoLight,
borderRadius: 8,
padding: 8,
marginBottom: 8,
},
introText: {
fontSize: 13,
lineHeight: 19,
color: colors.textSecondary,
},
section: {
marginBottom: 16,
},
sectionTitle: {
fontSize: 14,
fontWeight: '700',
color: colors.textPrimary,
marginBottom: 10,
},
card: {
backgroundColor: colors.white,
borderRadius: 8,
overflow: 'hidden',
},
row: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 14,
paddingVertical: 14,
},
rowBorder: {
borderBottomWidth: 1,
borderBottomColor: colors.borderLight,
},
rowBody: {
flex: 1,
marginRight: 12,
},
rowTitle: {
fontSize: 15,
fontWeight: '600',
color: colors.textPrimary,
},
rowTitleDisabled: {
color: colors.textHint,
},
rowDesc: {
marginTop: 3,
fontSize: 12,
lineHeight: 16,
color: colors.textSecondary,
},
noteCard: {
flexDirection: 'row',
alignItems: 'flex-start',
backgroundColor: colors.white,
borderRadius: 8,
padding: 8,
gap: 8,
},
noteText: {
flex: 1,
fontSize: 12,
lineHeight: 18,
color: colors.textSecondary,
},
});